import { _decorator, Component, Label, Node, RichText, ScrollView, UIOpacity } from 'cc'
import { Logger } from '../utils'
const { ccclass, property } = _decorator

@ccclass('minorPrefab')
export class MinorPrefab extends Component {

    #logger = new Logger('配角弹框')

    @property(Node)
    userName: Node

    @property(Node)
    context: Node

    get container() {
        return this.context.parent
    }

    get scroll() {
        return this.container.getComponent(ScrollView)
    }

    get nickName() {
        return this.userName.getComponent(Label)
    }

    get opacity() {
        return this.node.getComponent(UIOpacity)
    }

    get contextStr() {
        return this.context.getComponent(RichText)
    }


    print(text: string) {
        this.contextStr.string = ''

        return new Promise(resolve => {
            this.schedule(() => {
                this.contextStr.string += text[this.contextStr.string.length]
                if (this.contextStr.string.length == text.length) {
                    resolve(true)
                }
                const { y } = this.scroll.getMaxScrollOffset()
                const { y: scrollY } = this.scroll.getScrollOffset()
                this.#logger.debug('getMaxScrollOffset', y)
                this.#logger.debug('getScrollOffset', scrollY)
                this.scroll.scrollToBottom(0.1)
            }, 0.02, text.length - 1)
        })
    }
}


